﻿using System.Waf.Applications;
using System.Windows;
using System.Globalization;
using System.ComponentModel.Composition;

namespace TrackMe.View.Services
{
    [Export(typeof(IDialogService))]
    public class DialogService : IDialogService
    {
    	private static MessageBoxOptions MessageBoxOptions
        {
            get
            {
                return (CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft) ? MessageBoxOptions.RtlReading : MessageBoxOptions.None;
            }
        }


        public bool? ShowQuestion(string message)
        {
            MessageBoxResult result = MessageBox.Show(message, ApplicationInfo.ProductName, MessageBoxButton.YesNoCancel, 
                MessageBoxImage.Question, MessageBoxResult.Cancel, MessageBoxOptions);

            if (result == MessageBoxResult.Yes) { return true; }
            else if (result == MessageBoxResult.No) { return false; }

            return null;
        }

        public bool ShowYesNoQuestion(string message)
        {
            MessageBoxResult result = MessageBox.Show(message, ApplicationInfo.ProductName, MessageBoxButton.YesNo,
                MessageBoxImage.Question, MessageBoxResult.No, MessageBoxOptions);

            return result == MessageBoxResult.Yes;
        }

    	public void ShowError(string message)
    	{
    		MessageBox.Show(message, ApplicationInfo.ProductName);
    	}

    	public bool PopupDialog<T>(ViewModel<T> model) where T : IView
    	{
    		var win = new Window
    			{
					SizeToContent = SizeToContent.WidthAndHeight,
    				Title = model.View.ToString(), 
					Content = model.View,
    			};
			var result = win.ShowDialog() == true;
    	    win.Content = null;
            win.Close();
            //((UIElement)model.View).
    		return result;
    	}
    }
}
